<?php
/**
 *
 * Author: TheSmile
 * Date: 2023/7/27 14:42
 * Email: <594652523@qq.com>
 */

namespace App\Admin\Extensions\Action;

use App\Models\UserCoupon;
use Encore\Admin\Actions\RowAction;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class RowDelete extends RowAction
{

    public $name = '删除';

    //业务处理
    public function handle(Model $model)
    {
        // $model ...
        $id = $model->id;
        DB::beginTransaction();
        try {
            $model->delete();
            UserCoupon::where('coupon_id', $model->id)->update(['use' => 1, 'use_time' => time(), 'remark' => '后台删除优惠券,此券更改为已使用']);
            DB::commit();
            return $this->response()->success('操作成功')->refresh();
        } catch (\Exception $exception) {
            DB::rollBack();
            return $this->response()->error('操作失败');
        }
    }

    /**
     * 弹出确认框
     */
    public function dialog()
    {
        $this->confirm('确定删除？');
    }
}
